package cn.geminis.web.util;

import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;

import java.nio.charset.StandardCharsets;

/**
 * @author puddi
 */
public class ResponseUtils {

    private ResponseUtils() {
    }

    public static <T> ResponseEntity<T> createFileResponse(T content, String fileName, MediaType type) {
        var headers = new HttpHeaders();
        //下载显示的文件名，解决中文名称乱码问题
        String downloadFileName = null;
        downloadFileName = new String(fileName.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1);
        headers.setContentDispositionFormData("attachment", downloadFileName);
        headers.setContentType(type);
        return new ResponseEntity<>(content, headers, HttpStatus.CREATED);
    }
}
